require "test/unit"
class Test_Koans<Test::Unit::TestCase
  def my_global_method(a,b)
    a + b
  end
  def test_calling_global_methods
    assert_equal 5, my_global_method(2,3)
  end

  def test_calling_global_methods_without_parentheses
    result = my_global_method 2, 3
    assert_equal 5, result 
  end
  # 可以不用括号，但是用括号的话必须有对应的收括号

  def method_with_defaults(a, b=:default_value)
    [a,b]
  end

  def test_calling_with_default_values
    assert_equal [1,:default_value], method_with_defaults(1)
    assert_equal [1, 2],             method_with_defaults(1, 2)
  end
  #当b有对应值是就取对应值，没对应值就直接:default_values

  def method_with_var_args(*args)
    args
  end

  def test_calling_with_variable_arguments
    assert_equal [],     method_with_var_args()
    assert_equal [:one], method_with_var_args(:one)
    assert_equal [:one, :two], method_with_var_args(:one, :two)
   #将参数以array的形式显示
  end

  def method_with_explicit_return
    :a_non_return_value
    return :return_value
    :another_non_return_value
  end

  def test_method_with_explicit_return
    assert_equal :return_value, method_with_explicit_return 
  end
  #直接返回到 :return_value

  def method_without_explicit_return
    :a_non_return_value
    :return_value
  end

  def test_method_without_explicit_return
    assert_equal :return_value, method_without_explicit_return 
  end
  # 定义没有明确的返回值，返回到:return_value

  def my_same_class_method(a, b)
    a * b
  end
  
  def test_calling_methods_in_same_class
    assert_equal 12, my_same_class_method(3, 4) 
  end
  #定义将两个参数做乘法

  def test_calling_methods_in_same_class_with_explicit_receiver
    assert_equal 12, self.my_same_class_method(3, 4)
  end
  # 明确的方法调用

  def my_private_method
    "a secret"
  end
  private :my_private_method
   
  def test_calling_private_methods_without_receiver
    assert_equal "a secret", my_private_method
  end
  # 已经定义了方法，调用

  class Dog
    def name
      "Fido"
    end
   
    private
    def tail
      "tail"
    end
  end
  
  def test_calling_methods_in_other_objects_require_explicit_receiver
    rover = Dog.new
    assert_equal "Fido", rover.name
  end
  # 新建Dog类里面的name就为Fido
  def test_calling_private_methods_in_other_objects
    rover = Dog.new
    assert_raise(NoMethodError) do 
      rover.tail
    end
  end
  # 若为assert_equal 则为“tail”, 在这里为NoMethodError
end


